package com.nvwa.es_ik_remote.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("api")
public class ApiController {

    @GetMapping("nvwaWord/{wordlib}")
    public void remoteSetHotWord(HttpServletResponse response, @PathVariable("wordlib")Integer wordlib) throws FileNotFoundException {
        String filePath ="";
        if (wordlib == 1)
            filePath = "D:\\mine\\code\\tool\\Elastic\\ik\\nvwa_word_example.dic";
        File file = new File(filePath);
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()))){

            char[] userBuffer = new char[1024];
            int charsnum;
            while ((charsnum = bufferedReader.read(userBuffer)) != -1) {
                bufferedWriter.write(userBuffer, 0, charsnum);
            }

        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

